<?php
/*******************************************************************************
 * $Date:: Jan 10, 2011 10:05:59 PM                                            $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage ##Subpackage##
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Service_Locator
{
    /**
     * @var Lua_Service_Locator
     */
    private static $instance = null;
    
    /**
     * @var array
     */
    private $services;
    
    /**
     * @return Lua_Service_Locator
     */
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }

        return self::$instance;
    }
    
    /**
     * Get Lua Service by name
     * @param string $name
     */
    public static function get($name)
    {
        return self::getInstance()->getService($name);
    }

    public function __construct()
    {
        $this->services = array();
    }

    /**
     * Get service, if the service has not set, it returns default one
     * @param string $name
     * @return Lua_Service_Interface
     */
    public function getService($name)
    {
        if (!isset($this->services[$name])) {
            $serviceClass = 'Lua_Service_' . $name;
            $this->services[$name] = new $serviceClass();
        }

        return $this->services[$name];
    }

    /**
     * Set service
     * @param string $name
     * @param Lua_Service_Interface $service 
     */
    public function setService($name, Lua_Service_Interface $service)
    {
        $this->services[$name] = $service;
    }

    /**
     * Reset all services
     */
    public function resetServices()
    {
        $this->services = array();
    }

    /**
     * @return Lua_Service_Base
     */
    static public function getBaseService()
    {
        return self::get('Base');
    }
    
    /**
     * @return Lua_Service_User
     */
    static public function getUserService()
    {
        return self::get('User');
    }

}